/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.pobing.client.web;

import com.google.common.collect.Maps;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.pobing.client.feign.ProtobufService;
import com.pobing.protobuf.lib.revert.Revert;
import com.pobing.protobuf.lib.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author xfworld
 * @since create on 2021-12-03.
 **/
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    RestTemplate protobufTemplate;

    @Autowired
    ProtobufService protobufService;

    @GetMapping("/sendReq")
    public Map<String,Object> getData(@RequestParam long id) throws InvalidProtocolBufferException {
        Map<String,Object> ret = Maps.newHashMap();
        JsonFormat.Printer printer = JsonFormat.printer();
        User.Builder builder = User.newBuilder();
        builder.setId(id);
        builder.setUserName("test");
        builder.setPhone("1234567");
        builder.setMemo("OK");
       // RestTemplate restTemplate = new RestTemplate(Collections.singletonList(protobufHttpMessageConverter));
        ResponseEntity<Revert> response = protobufTemplate.postForEntity("http://127.0.0.1:10091/test/getTime",builder.build(),Revert.class);
        if(response!=null){
            Any any = response.getBody().getData();
            if(any.is(User.class)) {
                ret.put("result", printer.includingDefaultValueFields().print(any.unpack(User.class)));
            }
            ret.put("status",true);
        }else{
            ret.put("status",false);
        }

        return ret;
    }

    @GetMapping("/getFeign")
    public Map<String,Object> feign(@RequestParam long id) throws InvalidProtocolBufferException {
        Map<String,Object> ret = Maps.newHashMap();
        JsonFormat.Printer printer = JsonFormat.printer();
        User.Builder builder = User.newBuilder();
        builder.setId(id);
        builder.setUserName("test");
        builder.setPhone("1234567");
        builder.setMemo("OK");

        Revert response = protobufService.feign(builder.build());

        if(response!=null){
            Any any = response.getData();
            if(any.is(User.class)) {
                ret.put("result", printer.includingDefaultValueFields().print(any.unpack(User.class)));
            }
            ret.put("status",true);
        }else{
            ret.put("status",false);
        }

        return ret;
    }

}
